package com.ashlikun.frame.simple

import android.content.Intent
import android.graphics.Insets
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ashlikun.frame.simple.wheel.WellMainActivity
import com.ashlikun.supertoobar.Action
import com.ashlikun.supertoobar.Action.OnActionClick
import com.ashlikun.supertoobar.ImageAction
import com.ashlikun.supertoobar.SuperToolBar
import com.ashlikun.supertoobar.TextAction
import com.ashlikun.utils.AppUtils
import com.ashlikun.utils.ui.extend.getUserRootView
import com.ashlikun.utils.ui.modal.ToastUtils
import com.ashlikun.utils.ui.status.enableEdgeToEdgeX
import com.ashlikun.wheelview3d.LoopView
import com.ashlikun.xrecycleview.OnLoaddingListener
import com.ashlikun.xrecycleview.RefreshLayout
import com.ashlikun.xrecycleview.dip2px
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class MainActivity : AppCompatActivity(), RefreshLayout.OnRefreshListener, OnLoaddingListener {
    lateinit var recycleView: RecyclerView
    lateinit var supperToolBar: SuperToolBar
    var listData: MutableList<Boolean> = ArrayList()
    var adapter: RecyclerView.Adapter<*>? = null
    private val image =
        "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1535434196&di=584361192dd507b1778369caf125650b&src=http://pic31.nipic.com/20130723/7447430_105434565000_2.jpg"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AppUtils.init(application, "frame")
        for (i in 0..99) {
            listData.add(true)
        }
        setContentView(R.layout.activity_main)
        enableEdgeToEdgeX(isSetView = true)
        //这里会动态返回用户设置的底部高度
        recycleView = findViewById(R.id.recycleView)
        supperToolBar = findViewById(R.id.toolBar)
//        supperToolBar.setSafeArea()
        recycleView!!.postDelayed(Runnable {
            findViewById<View>(R.id.actionButton).isSelected = true
            //                findViewById(R.id.actionButton).requestFocus();
//                findViewById(R.id.actionButton).requestFocusFromTouch();
        }, 1000)
        supperToolBar!!.setBack(this)
        val itemAnimator = DefaultItemAnimator()
        itemAnimator.setChangeDuration(0)
        recycleView!!.setItemAnimator(itemAnimator)
        recycleView!!.setLayoutManager(LinearLayoutManager(this))
        recycleView!!.setAdapter(object : RecyclerView.Adapter<MyHolder?>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
                return MyHolder(LayoutInflater.from(this@MainActivity).inflate(R.layout.imageview_item, parent, false))
            }

            override fun getItemCount() = listData.size

            override fun onBindViewHolder(holder: MyHolder, position: Int) {}
        }.also { adapter = it })
        supperToolBar.addAction(
            ImageAction(supperToolBar, R.mipmap.ic_cj_clock)
                .setWidth(dip2px(this, 30))
                .setTintColor(-0x1000000)
                .set()
        )
        supperToolBar.addAction(
            ImageAction(supperToolBar, R.mipmap.ic_cj_clock)
                .setTintColor(-0x1000000)
                .set()
        )
        supperToolBar.setNotification(1, 7)
        supperToolBar.addAction(
            TextAction(supperToolBar, "分享分享分享")
                .setNotificationStrokeColor(-0xba7878)
                .setNotificationMini()
                .set()
        )
        supperToolBar.setOnActionClickListener(OnActionClick { index: Int, action: Action? -> ToastUtils.show("aaaaaaa$index") })
        val options = RequestOptions()
        options.transform(RoundedCorners(dip2px(this, 6)))
        val loopview: LoopView = findViewById<LoopView>(R.id.loopview)
        loopview.setListener({ item: Int, data: Any -> Log.e("wwwwwww", "$item  $data") })
        loopview.setLoopCanTouch({
            Log.e("wwwwwww", "wwwwwwww")
            false
        })
        run()
    }

    private fun run() {
        val loopview: LoopView = findViewById<LoopView>(R.id.loopview)
        recycleView.postDelayed(Runnable {
            Log.e("ssssssss", loopview.isSmoothScroll().toString() + "  " + loopview.isTouchScroll())
            run()
        }, 200)
    }

    internal inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imageView: ImageView

        init {
            imageView = itemView.findViewById(R.id.imageView)
        }
    }

    override fun onLoadding() {}
    override fun onRefresh() {}
    fun onStickClick(view: View?) {
        startActivity(Intent(this, WellMainActivity::class.java))
    }

    fun onClick(view: View?) {
        supperToolBar.setBackImgColor(-0x10000)
        val action: ImageAction = supperToolBar.getAction<ImageAction>(0)
        action.setDrawableId(R.drawable.material_back)
        action.setTintColor(-0x10000)
        action.setNotification(100)
        val textAction: TextAction = supperToolBar.getAction<TextAction>(2)
        textAction.setNotification(101)
    }
}
